package com.zmsoft.boxy.sfg.service;

import org.cnsoft.framework.beans.common.RESTResultBean;
import org.cnsoft.framework.utils.EmptyHelper;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.zmsoft.boxy.sfg.player.BoxySFGPlayer;
import com.zmsoft.persistent.sfg.Sfg661010PlayerFarm.Sfg661010PlayerFarmDBO;
import com.zmsoft.persistent.sfg.Sfg661021PlayerLand.Sfg661021PlayerLandDBO;
import com.zmsoft.persistent.sfg.Sfg661040PlayerPocket.Sfg661040PlayerPocketDBO;
import com.zmsoft.persistent.sfg.Sfg662010GameLand.Sfg662010GameLandDBO;
import com.zmsoft.persistent.sfg.Sfg662040GameCrops.Sfg662040GameCropsDBO;
import com.zmsoft.persistent.sfg.Sfg662041GameCropsGrow.Sfg662041GameCropsGrowDBO;
import com.zmsoft.persistent.sfg.Sfg662050GameShop.Sfg662050GameShopDBO;

/**
 * 超管登录
 * 
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 * @see ApiManagerLoginController
 */
@Service("SFGGameActionService") // 替换系统登录接口
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
//@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SFGGameActionService extends MySFGGameServiceSupport {

	/**
	 * 加载商店信息
	 * 
	 * @param player
	 * @return
	 * @throws Exception
	 */
	public RESTResultBean<BoxySFGPlayer> loadShop(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		result.setData(sfgGameInfoCacheService.loadGameShop(ONE));
		return result;
	}

	/**
	 * 商店购买信息
	 */
	public RESTResultBean<BoxySFGPlayer> doShopBuy(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();

		// 获得物品信息
		Sfg662050GameShopDBO gameShopCrop = sfgGameInfoCacheService.loadGameShopData(SFG_GAME_FARM_TYPE, player.getPlayerPropId());
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 用户口袋数据
		Sfg661040PlayerPocketDBO playerPocketProp = sfgPlayerInfoCacheService.loadPlayerPocketProp(player);

		if (gameShopCrop.getWaresPrice() > playerFarm.getCurMoney()) {// 金钱不足
			result.setCode(1);
			result.setMsg(FAIL_MESSAGE_Money);
			return result;
		}
		if (EmptyHelper.isEmpty(playerPocketProp)) {
			//保存物品
			playerPocketProp = new Sfg661040PlayerPocketDBO();
			playerPocketProp.setPlayerId(player.getUserId());
			playerPocketProp.setPropId(gameShopCrop.getId());
			playerPocketProp.setPropName(gameShopCrop.getWaresName());
			playerPocketProp.setPropType(gameShopCrop.getWaresTypeCode());
			playerPocketProp.prepareNumeric();
		}
		//开始购买物品
		{
			//修正物品数量
			playerPocketProp.setPropNum(playerPocketProp.getPropNum() + gameShopCrop.getWaresNum());
			//修正剩余金币
			playerFarm.setCurMoney(playerFarm.getCurMoney() - gameShopCrop.getWaresPrice());
			// 用户数据集中保存
			sfgPlayerInfoCacheService.savePlayer(player, playerFarm, null, null, playerPocketProp);
			//设定返回
			result.setData(playerFarm);
		}

		return result;
	}

	/**
	 * 作物种植信息
	 */
	public RESTResultBean<BoxySFGPlayer> doLandPlant(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 获得用户土地信息
		Sfg661021PlayerLandDBO playerLand = sfgPlayerInfoCacheService.loadPlayerLand(player);
		// 用户口袋数据
		Sfg661040PlayerPocketDBO playerPocketProp = sfgPlayerInfoCacheService.loadPlayerPocketProp(player);
		// 1未开垦2未种植3成长中5成熟待收割9收割后未翻地)
		if (EmptyHelper.isEmpty(playerFarm) || EmptyHelper.isEmpty(playerLand) || EmptyHelper.isEmpty(playerPocketProp)) {
			result.setCode(1);
			result.setMsg(FAIL_MESSAGE_BUG);
			return result;
		}
		// 合理化检查
		if (TWO.equals(playerLand.getLandState()) == false) {
			result.setCode(1);
			result.setMsg(FAIL_ACTION_sow);
			return result;
		}
		// 获得物品信息
		Sfg662040GameCropsDBO gameCrop = sfgGameInfoCacheService.loadGameCropsData(SFG_GAME_FARM_TYPE, playerPocketProp.getPropId());
		if (EmptyHelper.isEmpty(gameCrop)) {
			result.setCode(1);
			result.setMsg(FAIL_MESSAGE_BUG);
			return result;
		}
		// 检查需要金钱、等级、道具
		{
			if (gameCrop.getNeedMoney() > playerFarm.getCurMoney()) {// 金钱不足
				result.setCode(1);
				result.setMsg(FAIL_MESSAGE_Money);
				return result;
			}
			if (gameCrop.getNeedFarmLevel() > playerFarm.getFarmLevel()) {// 农场等级不足
				result.setCode(1);
				result.setMsg(FAIL_MESSAGE_FarmLevel);
				return result;
			}
			if (gameCrop.getNeedExp() > playerFarm.getTotalExp()) {// 消耗经验
				result.setCode(1);
				result.setMsg(FAIL_MESSAGE_Exp);
				return result;
			}
			{// 时间秒
			}
		}
		// 扣除需要金钱和经验、道具
		{
			playerFarm.setCurMoney(playerFarm.getCurMoney() - gameCrop.getNeedMoney());
			SFGGameThrusterServiceHelper.preparePlayerFarmTodayExp(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFarm, gameCrop.getNeedExp() * (-1L));
			playerLand.setLandState(THREE);
		}
		// 扣除口袋物品
		if (playerPocketProp.getPropNum() > 0) {
			playerPocketProp.setPropNum(playerPocketProp.getPropNum() - 1);
		} else {
			result.setCode(1);
			result.setMsg(FAIL_MESSAGE_Pocket);
			return result;
		}
		// 整理植物基本数据到土地下面
		{
			// 作物ID
			playerLand.setCropsId(gameCrop.getId());
			// 作物名称
			playerLand.setCropsName(gameCrop.getCropsName());
			// 作物等级
			playerLand.setCropsLevel(gameCrop.getCropsLevel());
			// 成熟需要时间秒
			playerLand.setCropsNeedTime(gameCrop.getNeedTime());
			// 种植时间-秒
			playerLand.setPlantTime(System.currentTimeMillis());// 开始时间
			// 季数
			playerLand.setSeasonsNum(1L);
		}
		// 整理植物阶段数据到土地下面
		Sfg662041GameCropsGrowDBO gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, playerPocketProp.getPropId(), ONE);
		if (EmptyHelper.isEmpty(gameCropsGrowDBO)) {
			result.setCode(1);
			result.setMsg(FAIL_MESSAGE_BUG);
			return result;
		}
		{
			// 当前阶段ID
			playerLand.setStageId(gameCropsGrowDBO.getId());
			// 当前阶段名称
			playerLand.setStageName(gameCropsGrowDBO.getStageName());
			// 土地图片
			playerLand.setLandPicUrl(gameCropsGrowDBO.getStagePicUrl());
			// 当前阶段需要时间(秒) need_time
			playerLand.setNeedTime(gameCropsGrowDBO.getNeedTime());
			playerLand.setCropsNeedTime1(gameCropsGrowDBO.getNeedTime());

			// 事件触发
			SFGGameThrusterServiceHelper.preparePlayerLandCropsState(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerLand, gameCrop, gameCropsGrowDBO);

			// 动作增加经验
			SFGGameThrusterServiceHelper.preparePlayerFarmTodayExp(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFarm, gameCropsGrowDBO.getCropsExp());
		}

		// 用户数据集中保存
		sfgPlayerInfoCacheService.savePlayer(player, playerFarm, playerLand, null, playerPocketProp);

		result.setMsg(ACTION_OK_MESSAGE);
		result.setData(playerLand);// 数据刷新展示

		// 补全各个阶段时间
		prepareLandCropsGrow(player, playerFarm, playerLand);

		return result;
	}

	// 补全各个阶段时间
	@Async("threadPoolTaskExecutor")
	public void prepareLandCropsGrow(BoxySFGPlayer player, Sfg661010PlayerFarmDBO farm, Sfg661021PlayerLandDBO land) throws Exception {
		// 1土地种子
		Sfg662041GameCropsGrowDBO gameCropsGrowDBO;
		// 2幼苗发芽
		gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, land.getCropsId(), TWO);
		if (EmptyHelper.isNotEmpty(gameCropsGrowDBO)) {
			// 当前阶段需要时间(秒) need_time
			land.setCropsNeedTime2(gameCropsGrowDBO.getNeedTime());
		}
		// 3茁壮成长
		gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, land.getCropsId(), THREE);
		if (EmptyHelper.isNotEmpty(gameCropsGrowDBO)) {
			// 当前阶段需要时间(秒) need_time
			land.setCropsNeedTime3(gameCropsGrowDBO.getNeedTime());
		}
		// 4长大开花
		gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, land.getCropsId(), FOUR);
		if (EmptyHelper.isNotEmpty(gameCropsGrowDBO)) {
			// 当前阶段需要时间(秒) need_time
			land.setCropsNeedTime4(gameCropsGrowDBO.getNeedTime());
		}
		// 5成熟结果
		gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, land.getCropsId(), FIVE);
		if (EmptyHelper.isNotEmpty(gameCropsGrowDBO)) {
			// 当前阶段需要时间(秒) need_time
			land.setCropsNeedTime5(gameCropsGrowDBO.getNeedTime());
		}
		// 6果实收割

		// 补全土地
		sfgPlayerInfoCacheService.preparePlayerFarmLand(farm);

		// 保存数据
		sfgPlayerInfoCacheService.savePlayerFarm(farm);
		sfgPlayerInfoCacheService.savePlayerLand(player, land);
	}

	/**
	 * 土地开垦信息(从无到有)[下一步种植]
	 * 
	 * @throws Exception
	 */
	public RESTResultBean<BoxySFGPlayer> doLandReclamation(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 获得用户土地信息
		Sfg661021PlayerLandDBO playerLand = sfgPlayerInfoCacheService.loadPlayerLand(player);
		// 土地卡、经验值、好友
		// 1未开垦2未种植3成长中5成熟待收割9收割后未翻地)
		if (ONE.equals(playerLand.getLandState()) == false) {
			result.setCode(1);
			result.setMsg(FAIL_ACTION_bring);
			return result;
		}
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 获得土地升级配置
		Long nextLand = playerFarm.getLandNumUse() + 1;
		Sfg662010GameLandDBO gameLand = sfgGameInfoCacheService.loadGameLandData(SFG_GAME_FARM_TYPE, nextLand);
		// 检查需要金钱、等级、道具
		{
			if (gameLand.getNeedMoney() > playerFarm.getCurMoney()) {// 金钱不足
				result.setCode(1);
				result.setMsg(FAIL_MESSAGE_Money);
				return result;
			}
			if (gameLand.getNeedFarmLevel() > playerFarm.getFarmLevel()) {// 农场等级不足
				result.setCode(1);
				result.setMsg(FAIL_MESSAGE_FarmLevel);
				return result;
			}
		}
		// 扣除需要金钱和经验、道具
		{
			playerFarm.setCurMoney(playerFarm.getCurMoney() - gameLand.getNeedMoney());
			playerLand.setLandState(TWO);// 更新土地状态
			//动作增加经验, 农场升级信息
			// TODO 等待转到配置
			SFGGameThrusterServiceHelper.preparePlayerFarmTodayExp(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFarm, 5);
		}
		// 用户数据集中保存
		sfgPlayerInfoCacheService.savePlayer(player, playerFarm, playerLand, null, null);
		result.setData(playerLand);// 数据刷新展示
		return result;
	}

	/**
	 * 浇水
	 * 
	 * @param init
	 * @return
	 * @throws Exception 
	 */
	public RESTResultBean<BoxySFGPlayer> doLandPlantWatering(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 获得用户土地信息
		Sfg661021PlayerLandDBO playerLand = sfgPlayerInfoCacheService.loadPlayerLand(player);
		// 用户口袋数据
		Sfg661040PlayerPocketDBO playerPocketProp = sfgPlayerInfoCacheService.loadPlayerPocketProp(player);
		

		// 动作增加经验, 农场升级信息
		SFGGameThrusterServiceHelper.preparePlayerFarmTodayExp(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFarm, 1);
		return result;
	}

//	// 种植
//	public final static String Interact_Command_doLandPlant = "doLandPlant";
//	// 施肥
//	public final static String Interact_Command_doLandPlantFertilize = "doLandPlantFertilize";
//	// 杀虫
//	public final static String Interact_Command_doLandPlantInsecticidal = "doLandPlantInsecticidal";
//	// 摘取果实
//	public final static String Interact_Command_doLandPlantPick = "doLandPlantPick";
//	// 浇水
//	public final static String Interact_Command_doLandPlantWatering = "doLandPlantWatering";
//	// 除草
//	public final static String Interact_Command_doLandPlantWeed = "doLandPlantWeed";
//	// 开垦
//	public final static String Interact_Command_doLandReclamation = "doLandReclamation";
//	// 翻地
//	public final static String Interact_Command_doLandTurn = "doLandTurn";
	
	
	
	/**
	 * 施肥
	 * 
	 * @param init
	 * @return
	 */
	public RESTResultBean<BoxySFGPlayer> doLandPlantFertilize(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 获得用户土地信息
		Sfg661021PlayerLandDBO playerLand = sfgPlayerInfoCacheService.loadPlayerLand(player);
		// 用户口袋数据
		Sfg661040PlayerPocketDBO playerPocketProp = sfgPlayerInfoCacheService.loadPlayerPocketProp(player);
		
		
		return result;
	}

	/**
	 * 除草
	 * 
	 * @param init
	 * @return
	 */
	public RESTResultBean<BoxySFGPlayer> doLandPlantWeed(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 获得用户土地信息
		Sfg661021PlayerLandDBO playerLand = sfgPlayerInfoCacheService.loadPlayerLand(player);
		// 用户口袋数据
		Sfg661040PlayerPocketDBO playerPocketProp = sfgPlayerInfoCacheService.loadPlayerPocketProp(player);
		
		
		return result;
	}

	/**
	 * 杀虫
	 * 
	 * @param init
	 * @return
	 */
	public RESTResultBean<BoxySFGPlayer> doLandPlantInsecticidal(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 获得用户土地信息
		Sfg661021PlayerLandDBO playerLand = sfgPlayerInfoCacheService.loadPlayerLand(player);
		// 用户口袋数据
		Sfg661040PlayerPocketDBO playerPocketProp = sfgPlayerInfoCacheService.loadPlayerPocketProp(player);
		
		
		return result;
	}

	/**
	 * 摘取果实
	 */
	public RESTResultBean<BoxySFGPlayer> doLandPlantPick(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 获得用户土地信息
		Sfg661021PlayerLandDBO playerLand = sfgPlayerInfoCacheService.loadPlayerLand(player);
		// 用户口袋数据
		Sfg661040PlayerPocketDBO playerPocketProp = sfgPlayerInfoCacheService.loadPlayerPocketProp(player);
		
		
		return result;
	}

	/**
	 * 翻地
	 * 
	 * @param init
	 * @return
	 */
	public RESTResultBean<BoxySFGPlayer> doLandTurn(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 获得用户土地信息
		Sfg661021PlayerLandDBO playerLand = sfgPlayerInfoCacheService.loadPlayerLand(player);
		// 用户口袋数据
		Sfg661040PlayerPocketDBO playerPocketProp = sfgPlayerInfoCacheService.loadPlayerPocketProp(player);
		
		
		return result;
	}

}
